﻿using System;
using System.Web.Caching;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using BikeBouncer.BusinessLayer;
using BikeBouncer.Site.Enums;

namespace BikeBouncer.Site
{
	public class BasePage : Page
	{
		#region Properties

		protected String Description { get; set; }

		protected String H1
		{
			get
			{
				return ((MainLayout_master)Master).H1;
			}
			set
			{
				((MainLayout_master)Master).H1 = value;
			}
		}

		protected String ErrorTitle
		{
			get
			{
				return ((MainLayout_master)Master).ErrorTitle;
			}
			set
			{
				((MainLayout_master)Master).ErrorTitle = value;
			}
		}

		protected Int32 ErrorCount
		{
			get
			{
				return ((MainLayout_master)Master).Errors.Controls.Count;
			}
		}

		protected Ads_ascx FooterAds
		{
			get
			{
				return ((MainLayout_master)Master).FooterAds;
			}
		}

		#endregion

		#region Custom viewstate

		protected override PageStatePersister PageStatePersister
		{
			get
			{
				return new SessionPageStatePersister(this);
			}
		}

		#endregion

		#region Main methods

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			Object title = GetLocalResourceObject("PageTitle");
			Object description = GetLocalResourceObject("PageDescription");
			Object h1 = GetLocalResourceObject("PageH1");

			if (title != null)
			{
				Title = title.ToString();
			}

			if (description != null)
			{
				Description = description.ToString();
			}

			if (h1 != null)
			{
				H1 = h1.ToString();
			}
		}

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);

			String defaultTitle = GetGlobalResourceObject("Messages", "DefaultPageTitle").ToString();

			if (String.IsNullOrEmpty(Title))
			{
				Title = defaultTitle;
			}
			else
			{
				Title = Title + " - " + defaultTitle;
			}

			((HtmlMeta)Header.FindControl("MetaDescription")).Content = Description;
		}

		#endregion

		#region Append scripts and styles

		protected void RegisterCss(String url)
		{
			HtmlGenericControl css = new HtmlGenericControl("link");

			css.Attributes.Add("type", "text/css");
			css.Attributes.Add("rel", "stylesheet");
			css.Attributes.Add("href", url);

			Page.Header.Controls.Add(css);
		}

		protected void RegisterScript(String url)
		{
			HtmlGenericControl js = new HtmlGenericControl("script");

			js.Attributes.Add("type", "text/javascript");
			js.Attributes.Add("src", url);

			Page.Header.Controls.Add(js);
		}

		protected void RegisterScript(ScriptSource script)
		{
			switch (script)
			{
				case ScriptSource.ColorPicker:
					RegisterScript("/Scripts/jquery.iColorPicker.js");
					break;
				case ScriptSource.ColorBox:
					RegisterScript("/Scripts/jquery.colorbox-min.js");
					RegisterCss("Styles/ColorBox.css");
					break;
				case ScriptSource.ToolTips:
					if (Request.Browser.Browser == "IE")
					{
						RegisterScript("/Scripts/excanvas.compiled.js");
					}

					RegisterScript("/Scripts/jquery.hoverIntent.min.js");
					RegisterScript("/Scripts/jquery.bt.min.js");
					break;
			}
		}

		#endregion

		#region Caching

		protected void SetCache(String key, Object value, CacheDuration duration)
		{
			Cache.Insert(key, value, null, DateTime.Now.AddMinutes((Int32)duration), Cache.NoSlidingExpiration);
		}

		protected Object GetCache(String key)
		{
			return Cache.Get(key);
		}

		#endregion

		#region Errors

		public void AppendError(Exception ex)
		{
			AppendError(GetGlobalString("Error") + ": " + ex.Message);
		}

		public void AppendError(Exception ex, String prefix)
		{
			AppendError(prefix + ": " + ex.Message);
		}

		public void AppendError(String message)
		{
			HtmlGenericControl li = new HtmlGenericControl("li");
			li.InnerHtml = message;

			((MainLayout_master)Master).Errors.Controls.Add(li);
		}

		#endregion

		#region Get ID from query

		protected Int32 GetIdFromQuery(String name)
		{
			String q = Request.QueryString[name];

			if (String.IsNullOrEmpty(q))
			{
				return 0;
			}

			try
			{
				return Convert.ToInt32(q.Split("-".ToCharArray())[0]);
			}
			catch
			{
				return 0;
			}
		}

		#endregion

		#region Resources

		public String GetGlobalString(String key)
		{
			return GetGlobalResourceObject("Messages", key).ToString();
		}

		#endregion

		#region Page redirection

		protected void LoginBlockedRedirect()
		{
			Response.Redirect("/Error.aspx?loginblocked");
		}

		protected void DataInvalidRedirect()
		{
			Response.Redirect("/Error.aspx?datainvalid");
		}

		#endregion
	}
}